﻿namespace EFS.RC.CCAdmin.Surveys.Question
{
	/// Class List
	public partial class List : EFS.RC.CCAdmin.Common.Control
	{
		#region Locals
		// parent category
		private System.Int32? categoryId = null;
		#endregion

		#region Constructors

		/// Implicit
		public List()
		{
			InitializeComponent();

			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.SurveyQuestion_GetByCategoryIDCompleted += UtilitiesService_SurveyQuestion_GetByCategoryIDCompleted;

				//set delete response listener
				UtilitiesService.SurveyQuestion_DeleteCompleted += UtilitiesService_SurveyQuestion_DeleteCompleted;
			}
		}

		/// <summary>
		/// Set id
		/// </summary>
		/// <param name="categoryId"></param>
		public void SetCategoryId(System.Int32? categoryId)
		{
			this.categoryId = categoryId;
			LoadData();
		}

		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			if (categoryId.HasValue)
			{
				// Load questions
				LoadQuestions();
			}
		}

		/// LoadQuestions
		private void LoadQuestions()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

			//call ws
			UtilitiesService.SurveyQuestion_GetByCategoryIDAsync(categoryId.Value);
		}
		#endregion

		#region WS callbacks
		/// Get all questions completed
		private void UtilitiesService_SurveyQuestion_GetByCategoryIDCompleted(object sender, WebProxy.SurveyQuestion_GetByCategoryIDCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to retrieve data.", e.Error);

				// failes
				return;
			}

			// check any
			if (null == e.Result)
			{
				return;
			}
			try
			{
				// deserialize
				dataGridQuestions.ItemsSource = EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Question[]>(e.Result.ToString());
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unable to load data. " + ex.Message);
			}
		}

		/// Delete question completed
		private void UtilitiesService_SurveyQuestion_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			if (null != e.Error)
			{
				ShowErrorMessage("Failed to delete question.", e.Error);
			}
			else
			{
				// Load questions
				LoadQuestions();
			}
		}
		#endregion

		#region CRUD Click listeners

		//edit question button click in datagrid
		private void EditLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.RC.CC.CH.Entities.Surveys.Question question = (EFS.RC.CC.CH.Entities.Surveys.Question) dataGridQuestions.SelectedItem;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Surveys.Question.Edit(wnd, categoryId.Value, question.ID);
			wnd.Show(OnEditWindowClosed);
		}

		//delete button click in datagrid
		private void DeleteLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete question?"))
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while deleting question");

				EFS.RC.CC.CH.Entities.Surveys.Question question = (EFS.RC.CC.CH.Entities.Surveys.Question)dataGridQuestions.SelectedItem;
				UtilitiesService.SurveyQuestion_DeleteAsync(question.ID.Value);
			}
		}

		#endregion

		#region Window callbacks
		//add new question
		private void OnNewWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			// load questions
			LoadQuestions();
		}

		//update question
		private void OnEditWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				return;
			}

			// load questions
			LoadQuestions();
		}
		#endregion

		#region Events
		/// Tool bar click
		private void ToolbarItem_New_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// check sanity
			if (categoryId.HasValue)
			{
				EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
				wnd.HostedContent = new EFS.RC.CCAdmin.Surveys.Question.Edit(wnd, categoryId.Value, null);
				wnd.Show(OnNewWindowClosed);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}